package com.school.express.controller;

import com.github.pagehelper.PageInfo;
import com.school.express.domain.Express;
import com.school.express.domain.User;
import com.school.express.domain.UserExpress;
import com.school.express.model.ExpressModel;
import com.school.express.service.ExpressService;
import com.school.express.service.UserService;
import com.school.support.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by 执笔 on 2017/3/22.
 */
@Controller
@RequestMapping("express")
public class ExpressController extends BaseController {

    @Autowired
    private ExpressService expressService;
    @Autowired
    private UserService userService;

    @RequestMapping("list")
    public String list(Model model, Express express, Page page) {
        Example example = ExampleUtil.getInstance()
                .addParam("no", express.getNo(), ExampleType.Operation.LIKE)
                .addParam("status", express.getStatus())
                .addOrder("addtime", ExampleType.OrderType.DESC)
                .getExample();
        PageInfo<Express> pageInfo = expressService.selectByExample(example, page);
        setModelAttribute(model, pageInfo);
        User user = new User();
        user.setType("kdy");
        user.setStatus(STAUTS_1);
        List<User> userList = userService.select(user);
        model.addAttribute(userList);
        return "express/list";
    }

    @RequestMapping("add")
    public String add() {
        return "express/add";
    }


    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(Express express) {
        Express expressTemp = new Express();
        expressTemp.setNo(express.getNo());
        if (null != expressService.selectOne(expressTemp)) {
            throw new MessageException("该编号已经存在");
        }
        express.setAddtime(new Date());
        expressService.insertSelective(express);
        return redirect("list");
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Express express = expressService.selectByPK(id);
        if (null == express) {
            throw new MessageException("该快递不存在");
        }
        model.addAttribute(express);
        return "express/detail";
    }

    @RequestMapping("update")
    public String update(Express express) {
        Express expressTemp = new Express();
        expressTemp.setNo(express.getNo());
        expressTemp = expressService.selectOne(expressTemp);
        if (null != expressTemp && !Objects.equals(express.getId(), expressTemp.getId())) {
            throw new MessageException("该编号已经存在");
        }
        expressService.updateByIdSelective(express);
        return redirect("list");
    }

    @RequestMapping("chuku")
    public String chuku(UserExpress userExpress) {
        expressService.chuku(userExpress);
        return refresh();
    }

    @RequestMapping("tranList")
    public String tranList(Model model, Express express, Page page) {
        List<Integer> list = new ArrayList<>();
        list.add(STAUTS_2);
        expressList(model, express, page, list);
        return "express/tran-list";
    }

    @RequestMapping("backList")
    public String backList(Model model, Express express, Page page) {
        List<Integer> list = new ArrayList<>();
        list.add(STAUTS_4);
        list.add(STAUTS_5);
        list.add(STAUTS_6);
        expressList(model, express, page, list);
        return "express/back-list";
    }

    @RequestMapping("completeList")
    public String completeList(Model model, Express express, Page page) {
        Example example = ExampleUtil.getInstance()
                .addParam("no", express.getNo(), ExampleType.Operation.LIKE)
                .addParam("e.status", STAUTS_3)
                .addParam("user_id", sessionUser().getId())
                .addOrder("addtime", ExampleType.OrderType.DESC)
                .getExample();
        PageInfo<ExpressModel> pageInfo = expressService.selectModel(example, page);
        setModelAttribute(model, pageInfo);
        return "express/complete-list";
    }

    private void expressList(Model model, Express express, Page page, List<Integer> status) {
        Example example = ExampleUtil.getInstance()
                .addParam("no", express.getNo(), ExampleType.Operation.LIKE)
                .addParam("e.status", status, ExampleType.Operation.IN)
                .addParam("user_id", sessionUser().getId())
                .addOrder("addtime", ExampleType.OrderType.DESC)
                .getExample();
        PageInfo<Express> pageInfo = expressService.selectByUser(example, page);
        setModelAttribute(model, pageInfo);
    }

    @RequestMapping("back/{id}")
    public String back(@PathVariable Integer id) {
        Express express = expressService.selectByPK(id);
        if (null == express) {
            throw new MessageException("该快递不存在");
        }
        express.setStatus(STAUTS_4);
        expressService.updateByIdSelective(express);
        return refresh();
    }

    @RequestMapping("verify")
    public String verify(Integer id,Integer status){
        Express express = expressService.selectByPK(id);
        if (null == express) {
            throw new MessageException("该快递不存在");
        }
        if (express.getStatus() != STAUTS_4) {
            throw new MessageException("该快递不是待审核状态");
        }
        express.setStatus(status);
        expressService.updateByIdSelective(express);
        return refresh();
    }
}
